home *** CD-ROM | disk | FTP | other *** search
- Path: gatekeeper.liffe.com!usenet
- From: ralph.mason@liffe.com (Ralph Mason)
- Newsgroups: comp.lang.c++
- Subject: Re: [help] 3D Array class
- Date: Thu, 25 Jan 1996 14:23:19 GMT
- Organization: London International Financial Futures Exchange
- Message-ID: <4e8413$10u@gatekeeper.liffe.com>
- References: <4e5sg8$r4h@geraldo.cc.utexas.edu>
- NNTP-Posting-Host: ralph_pc.liffe.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- tian@utpapa.ph.utexas.edu (Shiyang Tian) wrote:
-
- >Hi,
-
- >I'd like to construct a 3D array class which can take negative indices.
- >What's the best way to do it? A related question: is it possible to
-
- That depends on ths size of the array and what you are going to do
- with it.
-
- The simplest wat is to just have a regular array and subtracty an
- offset of any entries you make.
-
-
- >overload operator [][][] so that I can write something like following?
-
- >class array3D { ... };
-
- >array3D x;
- >array3D *y;
-
- >y = new array3D(X, Y. Z); // index for y is from -X to X, ...
- >(*y)[1][2][-3] = x[-1][-2][3];
-
- class int_a
- {
- public:
-
- int* a;
- int offset;
-
- int_a(int* pint,int off) { a=pint;offset=off; }
-
- int& operator[](int index)
- {
- return *(a+index );
- }
-
- };
-
- class array
- {
- private:
-
- int * pData;
- int xoff,yoff;
- int xrange,yrange;
-
- public:
-
- array ( int xmin, int xmax ,int ymin,int ymax )
- {
- xrange = xmax- xmin;
- yrange = ymax- ymin;
-
- pData = (int*) new char [ sizeof(int) * (xrange + 1 )* (yrange + 1)
- ];
-
- xoff = xmin;
- yoff = ymin;
- };
-
-
- int_a operator[](int indexx)
- {
- return int_a( pData+ ( yrange*( indexx-xoff ) ),yoff );
- }
-
-
- };
-
-
-
- int main()
- {
- array ar(-5,2,3,7);
-
- ar[0][3]=1;
- ar[-2][4]=7;
- printf("%d,%d",ar[0][3],ar[-2][4]);
-
- return 0;
- }
-
-
- This only does 2d arrays but can easily be converted to a 3d array of
- greater.
-
- Using a couple templates you should be able to extend this code to
- cover any sized arrays.
-
- I hope this is of some help
-
-
- this rambling is from ralph.mason@liffe.com
- -----------------------------------------------------------------------
- University of Liffe, London UK My views are my own!
-
-
-